package com.fortune.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fortune.base.BaseAPIExcution;
import com.fortune.common.paging.PagingData;
import com.fortune.constants.Constants;
import com.fortune.dto.excution.DOrderExcution;
import com.fortune.dto.in.dOrder.DOrderAddDto;
import com.fortune.dto.in.dOrder.DOrderEditDto;
import com.fortune.dto.in.dOrder.DOrderListDto;
import com.fortune.entity.DUserInfo;
import com.fortune.entity.SimpleManufactory;
import com.fortune.entity.SimpleProduct;
import com.fortune.enums.DOrderEnum;
import com.fortune.service.DOrderService;
import com.fortune.service.MDRelationService;
import com.fortune.service.MProductInfoService;
import com.fortune.service.PaymentMethodService;
import com.fortune.vo.dOrder.DOrderInfo;
import com.fortune.vo.paymentMethod.PaymentMethod;
import com.fortune.vo.dOrder.DOrderDetail;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

@Api(value = "dOrder", description = "经销商进货订单", tags = {"经销商进货订单"})
@RestController
@RequestMapping("dOrder")
public class DOrderController {
	@Autowired
	private DOrderService dOrderService;
	@Autowired
	private PaymentMethodService paymentMethodService;
	@Autowired
	private MDRelationService mDRelationService;
	@Autowired
	private MProductInfoService mProductInfoService;
	
	@ApiOperation(value = "订单列表", nickname = "dOrderList", notes = "", tags={ "@岳希伟" })
	@ApiImplicitParams({
		@ApiImplicitParam(name="page", value="第几页",paramType="query",dataType="int"),
		@ApiImplicitParam(name="length", value="每页容量",paramType="query",dataType="int"),
		@ApiImplicitParam(name="order", value="排序依据",paramType="query",dataType="string"),
		@ApiImplicitParam(name="type", value="排序类型(0升序,1降序,默认为1)",paramType="query",dataType="Integer",allowableValues = "0,1")
	})
    @GetMapping(value = "list",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<PagingData<DOrderInfo>> list(HttpServletRequest request,@Validated @ApiIgnore DOrderListDto listDto) {
		HttpSession session = request.getSession();
		DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		listDto.setPage((listDto.getPage()-1)*10);
		List<DOrderInfo> list = dOrderService.list(dUserInfo,listDto);
		int count = dOrderService.count(dUserInfo, listDto);
		PagingData<DOrderInfo> pagingData = new PagingData<>(list, count);
		return new DOrderExcution<PagingData<DOrderInfo>>(pagingData, DOrderEnum.SUCCESS);
	}
	
	@ApiOperation(value = "订单详情", nickname = "dOrderDetail", notes = "", tags={ "@岳希伟" })
	@ApiImplicitParams({
			@ApiImplicitParam(name="id", value="订单id",paramType="query",dataType="string",required=true),
			@ApiImplicitParam(name="mid", value="厂商id",paramType="query",dataType="string",required=true)
	})
    @GetMapping(value = "detail",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<DOrderDetail> detail(HttpServletRequest request,@Validated @NotBlank(message="订单id不能为空")String id,@Validated @NotBlank(message="厂商id不能为空")String mid) {
		HttpSession session = request.getSession();
		DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		dUserInfo.setmId(mid);
		DOrderDetail dOrderDetail = dOrderService.detail(dUserInfo, id);
		if (dOrderDetail==null) {
			return new DOrderExcution<DOrderDetail>(null, DOrderEnum.SUCCESS);
		}
		return new DOrderExcution<DOrderDetail>(dOrderDetail, DOrderEnum.SUCCESS);
	}
	
	@ApiOperation(value = "添加订单", nickname = "dOrderAdd", notes = "", tags={ "@岳希伟" })
    @PostMapping(value = "add",
        produces = { MediaType.APPLICATION_JSON_VALUE },
        consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> add(HttpServletRequest request,@Validated @RequestBody DOrderAddDto addDto) {
		HttpSession session = request.getSession();
		DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		dUserInfo.setmId(addDto.getmId());
		int result = dOrderService.add(addDto, dUserInfo);
		if(result==0){
			return new DOrderExcution<Void>(null, DOrderEnum.FALSE);
		}else if (result==2) {
			return new DOrderExcution<Void>(null, DOrderEnum.NOPRODUCT);
		}else if (result == -1){
			return new DOrderExcution<Void>(null, DOrderEnum.REPEAT);
		}
		return new DOrderExcution<Void>(null, DOrderEnum.SUCCESS);
	}
	
	@ApiOperation(value = "编辑订单", nickname = "dOrderEdit", notes = "", tags={ "@岳希伟" })
    @PutMapping(value = "edit",
        produces = { MediaType.APPLICATION_JSON_VALUE },
        consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> edit(HttpServletRequest request,@Validated @RequestBody DOrderEditDto editDto) {
		HttpSession session = request.getSession();
		DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		int result = dOrderService.edit(editDto, dUserInfo);
		if(result==0){
			return new DOrderExcution<Void>(null, DOrderEnum.FALSE);
		}else if (result==2) {
			return new DOrderExcution<Void>(null, DOrderEnum.NOPRODUCT);
		}
		return new DOrderExcution<Void>(null, DOrderEnum.SUCCESS);
	}
	
	@ApiOperation(value = "删除订单", nickname = "dOrderDelete", notes = "", tags={ "@岳希伟" })
	@ApiImplicitParams({
			@ApiImplicitParam(name="id", value="订单id",paramType="query",dataType="string",required=true),
			@ApiImplicitParam(name="mid", value="厂商id",paramType="query",dataType="string",required=true)
	})
    @DeleteMapping(value = "delete",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> delete(HttpServletRequest request,@Validated @NotBlank(message="订单id不能为空")String id,@Validated @NotBlank(message="厂商id不能为空")String mid) {
		HttpSession session = request.getSession();
		DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		dUserInfo.setmId(mid);
		int result = dOrderService.delete(dUserInfo, id);
		if(result==0){
			return new DOrderExcution<Void>(null, DOrderEnum.FALSE);
		}
		return new DOrderExcution<Void>(null, DOrderEnum.SUCCESS);
	}
	
	@ApiOperation(value = "支付方式列表", nickname = "paymentMethodListToUse", notes = "", tags={ "@曹然" })
    @GetMapping(value = "paymentMethods",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<List<PaymentMethod>> listPaymentMethods() {
		List<PaymentMethod> paymentMethods = paymentMethodService.listPaymentMethod();
		return new DOrderExcution<List<PaymentMethod>>(paymentMethods, DOrderEnum.SUCCESS);
	}
	
	@ApiOperation(value = "获取厂商列表", nickname = "mListToUse", notes = "", tags={ "@曹然" })
    @GetMapping(value = "manufactories",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<List<SimpleManufactory>> listManufactories(HttpServletRequest request) {
		HttpSession session = request.getSession();
		DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		String dId = dUserInfo.getdId();
		List<SimpleManufactory> simpleManufactories = mDRelationService.listRelationedManufactories(dId);
		SimpleManufactory simpleManufactory = new SimpleManufactory();
		return new DOrderExcution<List<SimpleManufactory>>(simpleManufactories, DOrderEnum.SUCCESS);
	}
	
	@ApiOperation(value = "获取商品列表", nickname = "pListToUse", notes = "", tags={ "@曹然" })
	@ApiImplicitParam(name="mId", value="厂商id",paramType="query",dataType="string",required=true)
    @GetMapping(value = "products",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<List<SimpleProduct>> listProducts(@Validated @NotBlank(message="厂商id不能为空") String mId) {
		List<SimpleProduct> products = mProductInfoService.listSimpleProductsByMId(mId);
		return new DOrderExcution<List<SimpleProduct>>(products, DOrderEnum.SUCCESS);
	}
}
